//
// Copyright (c) 2011, Peter Lane
// Licensed under the Academic Free License version 3.0

package fan.swtChartWrapper;

import fan.sys.*;
import org.eclipse.swtchart.Chart;
import org.eclipse.swtchart.IAxis;
import org.eclipse.swtchart.Range;

public class AxisPeer {
  private Chart _chart = null;
  private IAxis _axis = null;

  public static AxisPeer make (fan.swtChartWrapper.Axis self) throws Exception {
    return new AxisPeer ();
  }

  public void setChartAxis (Chart chart, IAxis axis) {
    _chart = chart;
    _axis = axis;
    _title.peer.setITitle (_axis.getTitle ());
    _grid.peer.setIGrid (_axis.getGrid ());
    _axis.setRange(new org.eclipse.swtchart.Range(_rangeMin, _rangeMax));
    _tick.peer.setIAxisTick (_axis.getTick ());
    if (_categories != null) setCategories ();
    _axis.enableLogScale (_logScale);
  }

  private Title _title = Title.make ();
  public Title title (fan.swtChartWrapper.Axis self) { return _title; }
  public void title (fan.swtChartWrapper.Axis self, Title t) {} // ignore this 

  private Grid _grid = Grid.make ();
  public Grid grid (fan.swtChartWrapper.Axis self) { return _grid; }
  public void grid (fan.swtChartWrapper.Axis self, Grid g) {} // ignore this

  private boolean _logScale = false;
  public boolean enableLogScale (fan.swtChartWrapper.Axis self) { return _logScale; }
  public void enableLogScale (fan.swtChartWrapper.Axis self, boolean v) {
    _logScale = v;
    if (_axis != null) _axis.enableLogScale (_logScale);
  }

  private boolean _setRange = false; // used to indicate if range set before display
  boolean rangeUnset () { return !_setRange; }
  private double _rangeMin = 0.0;
  private double _rangeMax = 1.0;
  public List range (fan.swtChartWrapper.Axis self) {
   if (_axis != null) {
     _rangeMin = _axis.getRange().lower;
     _rangeMax = _axis.getRange().upper;
   } 
    List values = List.make(Type.of(0.0), 2);
    values.add (_rangeMin);
    values.add (_rangeMax);
    return values;
  } 
  public void range (fan.swtChartWrapper.Axis self, List r) {
    if (r.size() == 2) {
      _rangeMin = (Double)r.get(0);
      _rangeMax = (Double)r.get(1);
      _setRange = true;
    }
    if (_axis != null) {
      _axis.setRange(new org.eclipse.swtchart.Range(_rangeMin, _rangeMax));
      _chart.redraw ();
    }
  }

  private AxisTick _tick = AxisTick.make ();
  public AxisTick tick (fan.swtChartWrapper.Axis self) { return _tick; }
  public void tick (fan.swtChartWrapper.Axis self, Object o) {} // ignore this

  private List _categories = List.make (Type.of(""), 0);
  public List categories (fan.swtChartWrapper.Axis self) { return _categories; }
  public void categories (fan.swtChartWrapper.Axis self, List givenCategories) {
    _categories = givenCategories.dup ();
    if (_axis != null) { setCategories (); }
  }
  private void setCategories () {
    if (!_categories.isEmpty ()) {
      _axis.setCategorySeries (Utility.toStringArray (_categories));
      _axis.enableCategory (true);
    }
  }

  public void scrollUp (fan.swtChartWrapper.Axis self) { 
    if (_axis != null && _chart != null) {
      _axis.scrollUp ();
      _chart.redraw ();
    }
  }

  public void scrollDown (fan.swtChartWrapper.Axis self) {
    if (_axis != null && _chart != null) {
      _axis.scrollDown ();
      _chart.redraw ();
    }
  }

  public void zoomIn (fan.swtChartWrapper.Axis self) {
    if (_axis != null && _chart != null) {
      _axis.zoomIn ();
      _chart.redraw ();
    }
  }

  public void zoomOut (fan.swtChartWrapper.Axis self) {
    if (_axis != null && _chart != null) {
      _axis.zoomOut ();
      _chart.redraw ();
    }
  }

  public void zoomInAt (fan.swtChartWrapper.Axis self, double coord) {
    if (_axis != null && _chart != null) {
      _axis.zoomIn (coord);
      _chart.redraw ();
    }
  }

  public void zoomOutAt (fan.swtChartWrapper.Axis self, double coord) {
    if (_axis != null && _chart != null) {
      _axis.zoomOut (coord);
      _chart.redraw ();
    }
  }

  public double dataCoordinate (fan.swtChartWrapper.Axis self, long coord) {
    if (_axis != null)
      return _axis.getDataCoordinate ((int)coord);
    else
      return 0.0; // throw error?
  }

  public long pixelCoordinate (fan.swtChartWrapper.Axis self, double coord) {
    if (_axis != null)
      return _axis.getPixelCoordinate (coord);
    else
      return 0; // throw error?
  }
}

